package com.jereh.weibo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jereh.weibo.R;
import com.jereh.weibo.circleimageview.CircleImageView;
import com.jereh.weibo.entity.List1;

import java.util.List;

/**
 * Created by Administrator on 2016/12/3.
 */
public class ListAdaprter extends BaseAdapter{

    private Context ctx;
    private List<List1> lists;

    public static final int LIST_VIEW_ONE=0;
    public static final int LIST_VIEW_TWO=1;
    public static final int LIST_VIEW_THREE=2;

    public ListAdaprter(Context ctx, List<List1> lists) {
        this.ctx = ctx;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if(lists.get(position).getImg21()==0&&lists.get(position).getImg31()==0){
            return LIST_VIEW_ONE;
        } else if(lists.get(position).getImg21()!=0){
            return LIST_VIEW_TWO;
        } else {
            return LIST_VIEW_THREE;
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1=null;
        ViewHolder2 holder2=null;
        ViewHolder3 holder3=null;
        int layoutType=getItemViewType(position);
        if(convertView==null){
            switch (layoutType){
                case LIST_VIEW_ONE:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.list_view_one,null,false);
                    holder1=new ViewHolder1();
                    holder1.img_tou=(CircleImageView)convertView.findViewById(R.id.img_tou);
                    holder1.name=(TextView)convertView.findViewById(R.id.name);
                    holder1.resource=(TextView)convertView.findViewById(R.id.resource);
                    holder1.content=(TextView)convertView.findViewById(R.id.content);
                    holder1.redirect=(TextView)convertView.findViewById(R.id.redirect);
                    holder1.comment=(TextView)convertView.findViewById(R.id.comment);
                    holder1.like=(TextView)convertView.findViewById(R.id.like);
//                    convertView.setTag(holder1);
                    convertView.setTag(R.id.noImg,holder1);
                    break;
                case LIST_VIEW_TWO:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.list_view_two,null,false);
                    holder2=new ViewHolder2();
                    holder2.img_tou=(CircleImageView)convertView.findViewById(R.id.img_tou);
                    holder2.name=(TextView)convertView.findViewById(R.id.name);
                    holder2.resource=(TextView)convertView.findViewById(R.id.resource);
                    holder2.content=(TextView)convertView.findViewById(R.id.content);
                    holder2.img1=(ImageView)convertView.findViewById(R.id.img21);
                    holder2.img2=(ImageView)convertView.findViewById(R.id.img22);
                    holder2.img3=(ImageView)convertView.findViewById(R.id.img23);
                    holder2.redirect=(TextView)convertView.findViewById(R.id.redirect);
                    holder2.comment=(TextView)convertView.findViewById(R.id.comment);
                    holder2.like=(TextView)convertView.findViewById(R.id.like);
//                    convertView.setTag(holder2);
                    convertView.setTag(R.id.oneImg,holder2);
                    break;
                case LIST_VIEW_THREE:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.list_view_three,null,false);
                    holder3=new ViewHolder3();
                    holder3.img_tou=(CircleImageView)convertView.findViewById(R.id.img_tou);
                    holder3.name=(TextView)convertView.findViewById(R.id.name);
                    holder3.resource=(TextView)convertView.findViewById(R.id.resource);
                    holder3.content=(TextView)convertView.findViewById(R.id.content);
                    holder3.img1=(ImageView)convertView.findViewById(R.id.img31);
                    holder3.img2=(ImageView)convertView.findViewById(R.id.img32);
                    holder3.img3=(ImageView)convertView.findViewById(R.id.img33);
                    holder3.img4=(ImageView)convertView.findViewById(R.id.img34);
                    holder3.img5=(ImageView)convertView.findViewById(R.id.img35);
                    holder3.img6=(ImageView)convertView.findViewById(R.id.img36);
                    holder3.img7=(ImageView)convertView.findViewById(R.id.img37);
                    holder3.img8=(ImageView)convertView.findViewById(R.id.img38);
                    holder3.img9=(ImageView)convertView.findViewById(R.id.img39);
                    holder3.redirect=(TextView)convertView.findViewById(R.id.redirect);
                    holder3.comment=(TextView)convertView.findViewById(R.id.comment);
                    holder3.like=(TextView)convertView.findViewById(R.id.like);
//                    convertView.setTag(holder3);
                    convertView.setTag(R.id.twoImg,holder3);
                    break;
            }
        }else {
            switch (layoutType){
                case LIST_VIEW_ONE:
                    holder1=(ViewHolder1)convertView.getTag(R.id.noImg);
                    break;
                case LIST_VIEW_TWO:
                    holder2=(ViewHolder2)convertView.getTag(R.id.oneImg);
                    break;
                case LIST_VIEW_THREE:
                    holder3=(ViewHolder3)convertView.getTag(R.id.twoImg);
                    break;
            }
        }
        List1 list=lists.get(position);
        switch (layoutType){
            case LIST_VIEW_ONE:
                holder1.img_tou.setImageResource(list.getImg_tou());
                holder1.name.setText(list.getName());
                holder1.resource.setText(list.getResource());
                holder1.content.setText(list.getContent());
                holder1.redirect.setText(list.getRedirect());
                holder1.comment.setText(list.getComment());
                holder1.like.setText(list.getLike());
                break;
            case LIST_VIEW_TWO:
                holder2.img_tou.setImageResource(list.getImg_tou());
                holder2.name.setText(list.getName());
                holder2.resource.setText(list.getResource());
                holder2.content.setText(list.getContent());
                holder2.img1.setImageResource(list.getImg21());
                holder2.img2.setImageResource(list.getImg22());
                holder2.img3.setImageResource(list.getImg23());
                holder2.redirect.setText(list.getRedirect());
                holder2.comment.setText(list.getComment());
                holder2.like.setText(list.getLike());
                break;
            case LIST_VIEW_THREE:
                holder3.img_tou.setImageResource(list.getImg_tou());
                holder3.name.setText(list.getName());
                holder3.resource.setText(list.getResource());
                holder3.content.setText(list.getContent());
                holder3.img1.setImageResource(list.getImg31());
                holder3.img2.setImageResource(list.getImg32());
                holder3.img3.setImageResource(list.getImg33());
                holder3.img4.setImageResource(list.getImg34());
                holder3.img5.setImageResource(list.getImg35());
                holder3.img6.setImageResource(list.getImg36());
                holder3.img7.setImageResource(list.getImg37());
                holder3.img8.setImageResource(list.getImg38());
                holder3.img9.setImageResource(list.getImg39());
                holder3.redirect.setText(list.getRedirect());
                holder3.comment.setText(list.getComment());
                holder3.like.setText(list.getLike());
                break;
        }
        return convertView;
    }

    static class ViewHolder1{
        CircleImageView img_tou;
        TextView name;
        TextView resource;
        TextView content;
        TextView redirect;
        TextView comment;
        TextView like;
    }
    static class ViewHolder2{
        CircleImageView img_tou;
        TextView name;
        TextView resource;
        TextView content;
        ImageView img1;
        ImageView img2;
        ImageView img3;
        TextView redirect;
        TextView comment;
        TextView like;
    }
    static class ViewHolder3{
        CircleImageView img_tou;
        TextView name;
        TextView resource;
        TextView content;
        ImageView img1;
        ImageView img2;
        ImageView img3;
        ImageView img4;
        ImageView img5;
        ImageView img6;
        ImageView img7;
        ImageView img8;
        ImageView img9;
        TextView redirect;
        TextView comment;
        TextView like;
    }

}
